package com.boarsoft.wechat.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;
import com.boarsoft.wechat.biz.MsgTypeBiz;
import com.boarsoft.wechat.biz.WeChatBiz;
import com.boarsoft.wechat.entity.MsgType;
import com.boarsoft.wechat.entity.WeChatTemplate;
import com.boarsoft.wechat.service.TempletService;

/**
 * 消息类型维护
 * 
 * @author xuhui.guo
 *
 */
@RestController
@RequestMapping("/msg")
public class MsgTypeAction {
	private static final Logger log = LoggerFactory.getLogger(MsgTypeAction.class);

	@Autowired
	private WeChatBiz weChatBiz;
	@Autowired
	private MsgTypeBiz msgTypeBiz;
	@Autowired
	private TempletService templateService;

	/**
	 * 
	 * @param templateCode
	 *            微信模板编码
	 * @param orderBy
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/list")
	public ReplyInfo<Object> list(String templateCode, String orderBy, int pageNo, int pageSize) {
		try {
			PagedResult<MsgType> types = msgTypeBiz.list(templateCode, orderBy, pageNo, pageSize);
			return new ReplyInfo<Object>(true, JsonUtil.from(types, "msgTypes"));
		} catch (Exception e) {
			log.error("Error to query msg type info.", e);
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN, new String[] { "查询消息类型" });
		}
	}

	/**
	 * 
	 * @param id
	 *            消息类型ID
	 * @param msgCode
	 *            消息类型
	 * @param weChatTemplateId
	 *            微信模板ID
	 * @param status
	 *            发布状态
	 * @param memo
	 *            备注
	 * @return
	 */
	@RequestMapping("/add")
	public ReplyInfo<Object> add(String id, String msgCode, String weChatTemplateId, String status, String memo) {
		if (Util.strIsEmpty(msgCode) || Util.strIsEmpty(weChatTemplateId)) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, new String[] { "参数" });
		}
		try {
			MsgType type = new MsgType();
			type.setMsgCode(msgCode);
			type.setStatus(MsgType.MSG_TYPE_TEMPLATE_STATUS_UNPUBLISH);
			type.setMemo(memo);

			WeChatTemplate template = weChatBiz.get(weChatTemplateId);
			template.getMsgTypes().add(type);

			type.setWechatTemplate(template);

			msgTypeBiz.save(type);

			return ReplyInfo.SUCCESS;
		} catch (Exception e) {
			log.error("Error to save msg type info.", e);
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN, new String[] { "保存消息类型" });
		}
	}

	@RequestMapping("/edit")
	public ReplyInfo<Object> edit(String id, String msgCode, String weChatTemplateId, String status, String memo) {
		if (Util.strIsEmpty(id) || Util.strIsEmpty(weChatTemplateId)) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, new String[] { "参数" });
		}
		try {
			MsgType type = new MsgType();
			type.setId(id);
			type.setStatus(status);
			type.setMsgCode(msgCode);
			type.setMemo(memo);

			WeChatTemplate template = weChatBiz.get(weChatTemplateId);

			type.setWechatTemplate(template);

			msgTypeBiz.save(type);

			if (MsgType.MSG_TYPE_TEMPLATE_STATUS_PUBLISH.equals(status)) {
				templateService.upadteMsgCode("2", msgCode, template.getTemplateId());
			}
			return ReplyInfo.SUCCESS;
		} catch (Exception e) {
			log.error("Error to save msg type info.", e);
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN, new String[] { "保存消息类型" });
		}
	}

	@RequestMapping("/delete")
	public ReplyInfo<Object> delete(String id, String msgCode, String weChatTemplateId, String status) {
		if (Util.strIsEmpty(id)) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, new String[] { "参数" });
		}
		try {
			msgTypeBiz.delete(id);

			WeChatTemplate template = weChatBiz.get(weChatTemplateId);

			if (MsgType.MSG_TYPE_TEMPLATE_STATUS_PUBLISH.equals(status)) {
				templateService.upadteMsgCode("3", msgCode, template.getTemplateId());
			}

			return ReplyInfo.SUCCESS;
		} catch (Exception e) {
			log.error("Error to delete msg type info.", e);
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN, new String[] { "删除消息类型" });
		}
	}

	@RequestMapping("/publish")
	public ReplyInfo<Object> publish(String id, String msgCode) {
		try {
			MsgType msgType = msgTypeBiz.get(id);
			msgType.setStatus(MsgType.MSG_TYPE_TEMPLATE_STATUS_PUBLISH);
			msgTypeBiz.save(msgType);
			templateService.upadteMsgCode("1", msgCode, msgType.getWechatTemplate().getTemplateId());
			return ReplyInfo.SUCCESS;
		} catch (Exception e) {
			log.error("Error to publish msg type info.", e);
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN, new String[] { "发布消息类型" });
		}
	}

	@RequestMapping("/uninstall")
	public ReplyInfo<Object> uninstall(String id, String msgCode) {
		try {
			MsgType msgType = msgTypeBiz.get(id);
			msgType.setStatus(MsgType.MSG_TYPE_TEMPLATE_STATUS_UNPUBLISH);
			msgTypeBiz.save(msgType);
			templateService.upadteMsgCode("3", msgCode, msgType.getWechatTemplate().getTemplateId());
			return ReplyInfo.SUCCESS;
		} catch (Exception e) {
			log.error("Error to uninstall msg type info.", e);
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN, new String[] { "卸载消息类型" });
		}
	}
}